repo: Avoid a possible divide by zero in progress
authorColin Walters <walters@verbum.org>
Fri, 17 Jun 2016 01:51:13 +0000 (21:51 -0400)
committerAtomic Bot <atomic-devel@projectatomic.io>
Fri, 17 Jun 2016 02:30:13 +0000 (02:30 +0000)
The previous code was subject to a divide by zero if less than a
second had passed.  Rework it so we only do the divide if more than a
second has passed.

Closes: #349
Approved by: Mathnerd314

src/libostree/ostree-repo.c

index 5e3e31df7b8ff78488edab020aec3b63e76642d1..e86685bf5f3e5ad3588b9a524b82df145abae9dc 100644 (file)
@@ -3865,20 +3865,23 @@ ostree_repo_pull_default_console_progress_changed (OstreeAsyncProgress *progress
       guint64 start_time = ostree_async_progress_get_uint64 (progress, "start-time");
       guint64 total_delta_part_size = ostree_async_progress_get_uint64 (progress, "total-delta-part-size");
       guint64 current_time = g_get_monotonic_time ();
-      guint64 bytes_sec = bytes_transferred / ((current_time - start_time) / G_USEC_PER_SEC);
-      guint64 est_time_remaining =  (total_delta_part_size - bytes_transferred) / bytes_sec;
       g_autofree char *formatted_bytes_transferred =
         g_format_size_full (bytes_transferred, 0);
       g_autofree char *formatted_bytes_sec = NULL;
       g_autofree char *formatted_est_time_remaining = NULL;
 
-      if (!bytes_sec) // Ignore first second
+      /* Ignore the first second, or when we haven't transferred any
+       * data, since those could cause divide by zero below.
+       */
+      if ((current_time - start_time) < G_USEC_PER_SEC || bytes_transferred == 0)
         {
           formatted_bytes_sec = g_strdup ("-");
           formatted_est_time_remaining = g_strdup ("- ");
         }
       else
         {
+          guint64 bytes_sec = bytes_transferred / ((current_time - start_time) / G_USEC_PER_SEC);
+          guint64 est_time_remaining =  (total_delta_part_size - bytes_transferred) / bytes_sec;
           formatted_bytes_sec = g_format_size (bytes_sec);
           formatted_est_time_remaining = _formatted_time_remaining_from_seconds (est_time_remaining);
         }